┌──[root@liruilongs.github.io]-[~] └─$ip netns exec net1 ip link 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: veth1@if2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether b2:ae:39:9e:50:4b brd ff:ff:ff:ff:ff:ff link-netns net2
net1 命名空间虚拟网卡 veth1 ,与名称为 net2 的命名空间相关联
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec net2 ip link 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: veth2@if2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 7a:1b:8e:91:41:79 brd ff:ff:ff:ff:ff:ff link-netns net1
┌──[root@liruilongs.github.io]-[~] └─$ip address add 192.168.20.1/24 dev veth1
激活本地回环网卡
1 2
┌──[root@liruilongs.github.io]-[~] └─$ip link set dev lo up
激活虚拟网卡
1 2
┌──[root@liruilongs.github.io]-[~] └─$ip link set dev veth1 up
因为另一端 veth1 还没有打开,所以链接状态仍然显示为关闭 state DOWN
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: veth1@if2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000 link/ether b2:ae:39:9e:50:4b brd ff:ff:ff:ff:ff:ff link-netns net2
查看路由信息,可以发现,命令空间路由相互独立,但是由于接口当前 down,这条路由实际不可用
1 2 3
┌──[root@liruilongs.github.io]-[~] └─$ip route 192.168.20.0/24 dev veth1 proto kernel scope link src 192.168.20.1 linkdown
查看命名空间 IP 信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
┌──[root@liruilongs.github.io]-[~] └─$ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: veth1@if2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether b2:ae:39:9e:50:4b brd ff:ff:ff:ff:ff:ff link-netns net2 inet 192.168.20.1/24 scope global veth1 valid_lft forever preferred_lft forever ┌──[root@liruilongs.github.io]-[~] └─$exit exit
退出第一个命名空间的 shell 环境,我们进入第二个命名空间的 shell 环境,做相同的配置 这里分配 IP 192.168.20.2/24
1 2 3 4 5 6 7 8
┌──[root@liruilongs.github.io]-[~] └─$ ip netns exec net2 bash ┌──[root@liruilongs.github.io]-[~] └─$ip address add 192.168.20.2/24 dev veth2 ┌──[root@liruilongs.github.io]-[~] └─$ip link set dev veth2 up ┌──[root@liruilongs.github.io]-[~] └─$ip link set dev lo up
这个时候,我们在看链接,状态,会发现,veth2 虚拟网卡状态为 UP 状态 state UP
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$ ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: veth2@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 7a:1b:8e:91:41:79 brd ff:ff:ff:ff:ff:ff link-netns net1
查看分配 IP 的虚拟网卡也为 UP 状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14
┌──[root@liruilongs.github.io]-[~] └─$ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: veth2@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 7a:1b:8e:91:41:79 brd ff:ff:ff:ff:ff:ff link-netns net1 inet 192.168.20.2/24 scope global veth2 valid_lft forever preferred_lft forever inet6 fe80::781b:8eff:fe91:4179/64 scope link valid_lft forever preferred_lft forever
独立的路由信息
1 2 3
┌──[root@liruilongs.github.io]-[~] └─$ip route 192.168.20.0/24 dev veth2 proto kernel scope link src 192.168.20.2
回到 net1,net1 名称空间中 veth1 的链接状态也显示 UP (state UP)
1 2 3 4 5 6 7 8 9
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec net1 ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: veth1@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether b2:ae:39:9e:50:4b brd ff:ff:ff:ff:ff:ff link-netns net2 ┌──[root@liruilongs.github.io]-[~] └─$exit exit
根命名空间不知道net1和net2命名空间的IP配置,三者彼此隔离。
1 2 3 4 5 6 7
┌──[root@liruilongs.github.io]-[~] └─$ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:93:51:67 brd ff:ff:ff:ff:ff:ff altname enp3s0
路由信息也为独立的路由信息
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$ip route default via 192.168.26.2 dev ens160 proto dhcp src 192.168.26.149 metric 100 192.168.26.0/24 dev ens160 proto kernel scope link src 192.168.26.149 metric 100
从根网络命名空间 ping 测试到 veth1 IP 失败。这是因为 IP 192.168.20.1 属于独立的网络命名空间 net1。
┌──[root@liruilongs.github.io]-[~] └─$ip netns list ┌──[root@liruilongs.github.io]-[~] └─$ip netns add red ┌──[root@liruilongs.github.io]-[~] └─$ip netns add green
用于在 Linux 上创建一个名为 vnet-br0 的桥接设备。桥接设备是用于连接多个网络设备的虚拟设备。它可以实现数据包的转发和交换,使得连接到桥接设备的网络设备可以相互通信。
1 2
┌──[root@liruilongs.github.io]-[~] └─$ip link add vnet-br0 type bridge
桥接设备(Bridge Device)是在网络层次结构中工作的二层设备(Data Link Layer),它主要用于连接多个网络设备,类似于网络交换机的功能。桥接设备通过学习和转发数据帧的方式,将连接到它的网络设备组成一个共享的以太网段,使得这些设备可以直接通信。桥接设备工作在数据链路层(第二层),它不涉及 IP 地址或路由。
通过 ip link 查看设备的状态、属性和配置信息
1 2 3 4 5 6 7 8 9
┌──[root@liruilongs.github.io]-[~] └─$ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:93:51:67 brd ff:ff:ff:ff:ff:ff altname enp3s0 5: vnet-br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether ce:93:3b:6d:37:48 brd ff:ff:ff:ff:ff:ff
可以看到刚刚添加的虚拟桥接设备,目前处于 DOWN 状态。
添加虚拟网卡对eth0-r 和 veth-r、eth0-g 和 veth-g
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$ip link add eth0-r type veth peer name veth-r ┌──[root@liruilongs.github.io]-[~] └─$ip link add eth0-g type veth peer name veth-g
把两个虚拟网卡对中的一端放到上面创建的网络命名空间
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$ip link set eth0-r netns red ┌──[root@liruilongs.github.io]-[~] └─$ip link set eth0-g netns green
然后将虚拟网卡对的另一端连接到vnet-br0桥。
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$ip link set veth-r master vnet-br0 ┌──[root@liruilongs.github.io]-[~] └─$ip link set veth-g master vnet-br0
查看根网络命名空间的桥接设备类型的网络设备(桥接表)。
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$ip link show type bridge 5: vnet-br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff
查看桥接设备(vnet-br0)关联的网络设备。
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$ip link show master vnet-br0 6: veth-r@if7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vnet-br0 state DOWN mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff link-netns red 8: veth-g@if9: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vnet-br0 state DOWN mode DEFAULT group default qlen 1000 link/ether be:a3:9a:1c:a1:06 brd ff:ff:ff:ff:ff:ff link-netns green
根据输出,有两个网络设备与 vnet-br0 桥接设备关联:
veth-r@if7:这是一个虚拟网络设备(veth pair),它与 vnet-br0 桥接设备关联。它的状态是 DOWN,表示当前处于未激活状态。它的 MAC 地址为 62:2b:41:f9:39:b3。此设备属于 red 网络命名空间。
veth-g@if9:这是另一个虚拟网络设备(veth pair),也与 vnet-br0 桥接设备关联。它的状态是 DOWN,表示当前处于未激活状态。它的 MAC 地址为 be:a3:9a:1c:a1:06。此设备属于 green 网络命名空间。
激活桥接对应的网络设备
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$ip link set vnet-br0 up ┌──[root@liruilongs.github.io]-[~] └─$ip link set veth-r up ┌──[root@liruilongs.github.io]-[~] └─$ip link set veth-g up
激活 网络命名空间中的回环地址和对应的虚拟网卡对
1 2 3 4 5 6 7 8
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ip link set lo up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ip link set eth0-r up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ip link set lo up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ip link set eth0-g up
ip link 确认设备状态
1 2 3 4 5 6 7 8 9 10 11 12 13
┌──[root@liruilongs.github.io]-[~] └─$ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:93:51:67 brd ff:ff:ff:ff:ff:ff altname enp3s0 5: vnet-br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff 6: veth-r@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master vnet-br0 state UP mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff link-netns red 8: veth-g@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master vnet-br0 state UP mode DEFAULT group default qlen 1000 link/ether be:a3:9a:1c:a1:06 brd ff:ff:ff:ff:ff:ff link-netns green
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ping 192.168.26.149 -c 3 PING 192.168.26.149 (192.168.26.149) 56(84) bytes of data. 64 bytes from 192.168.26.149: icmp_seq=1 ttl=64 time=0.241 ms 64 bytes from 192.168.26.149: icmp_seq=2 ttl=64 time=0.110 ms 64 bytes from 192.168.26.149: icmp_seq=3 ttl=64 time=0.075 ms
--- 192.168.26.149 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2074ms rtt min/avg/max/mdev = 0.075/0.142/0.241/0.071 ms ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ping 192.168.26.149 -c 3 PING 192.168.26.149 (192.168.26.149) 56(84) bytes of data. 64 bytes from 192.168.26.149: icmp_seq=1 ttl=64 time=0.258 ms 64 bytes from 192.168.26.149: icmp_seq=2 ttl=64 time=0.097 ms 64 bytes from 192.168.26.149: icmp_seq=3 ttl=64 time=0.094 ms
┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$ip link show type bridge 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT link/ether 02:42:68:f8:90:26 brd ff:ff:ff:ff:ff:ff ┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$ip link show master docker0